// 地址列表
var basePage = require('../../common/BasePage');
Page({
  behaviors: [basePage],
  /**
   * 页面的初始数据
   */
  data: {
    empty: false,
    loading: true,
    addrlist: [] ,//地址列表
    delete_pop:false,//是否显示删除地址确认框
    addr_id:0,//当前要删除得地址id
    addrIndex:0,//当前删除的地址索引
  },

  choose_click: function (e) { //选中地址
    console.log('choose_click', e);
    let addr=this.data.addrlist[e.currentTarget.dataset.index];
	console.log('choose_clickaddr', addr);
	this.default_add(addr)
    wx.setStorage({
      data: addr,
      key: 'addr',
    });
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.emit('change_addr', {
      addr: addr
    });
    wx.navigateBack({
      delta: 1,
    })
    // wx.navigateTo({
    //   url: '../tabbar/Tabbar?action=addr&tab_active=3',
    // })
  },
  default_click: function (e) { //设为默认按钮
    // console.log("default_click",e)
    let dataset = e.currentTarget.dataset.add;
    this.default_add(dataset)
  },
  default_add(dataset){
	this.request({
	  method: 'POST',
	  loadingTip: '更新中...',
	  url: this.data.host + '/home/user/new_addr/default',
	  data: {
	    addr_id: dataset.addr_id
	  }
	}, res => {
	  this.setdefault_ok(dataset.index)
	  // console.log("del_addr",res);
	});  
  },
  setdefault_ok(index) {
    wx.showToast({
      title: '设置成功'
    });
    this.request({
      url: this.data.host + '/home/user/new_addr/index?page=1&limit=100'
    }, this.get_list_ok);
  },
  delete_click: function ({currentTarget}) {
    // console.log("delete_click",e);
    this.setData({delete_pop:true,addr_id:currentTarget.dataset.id,addrIndex:currentTarget.dataset.index})
    // let dataset = e.currentTarget.dataset;
    
  },
  /**
   * 删除地址
   */
  delete_addr:function(){
    console.log("delete_addr",this.data.addr_id);
    this.request({
      method: 'POST',
      loadingTip: '删除中...',
      url: this.data.host + '/home/user/new_addr/delete',
      data: {
        addr_id: this.data.addr_id
      }
    }, res => {
      this.del_ok(this.data.addrIndex)
    });
  },
  del_ok: function (index) {
    wx.showToast({
      title: '删除成功'
    })
    this.data.addrlist.splice(index, 1)
    this.setData({
      addrlist: this.data.addrlist
    });
  },
  edit_click: function (e) {
    let addr = this.data.addrlist[e.currentTarget.dataset.index];
    wx.setStorageSync('addr', addr);
    wx.navigateTo({
      url: './add/AddAddr?state=update',
    })
  },
  add_click: function (e) {
    wx.navigateTo({
      url: './add/AddAddr',
      complete: function (res) {
        console.log(res)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.login_reload();
  },
  /**登陆刷新时重载)*/
  login_reload(){
    //获取地址列表
    this.setData({
      loading: true
    })
    this.request({
      url: this.data.host + '/home/user/new_addr/index'
    }, this.get_list_ok, null, res => {
      this.setData({
        loading: false
      })
    });

  },
  get_list_ok: function (res) {
    if (!res.data || res.data.length == 0) {
      this.setData({
        empty: true
      })
      return;
    }
    this.setData({
      addrlist: res.data
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})